package be.bonamis.umpire.business.mapping;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import be.bonamis.projects.umpire.business.dto.AnswerDTO;
import be.bonamis.projects.umpire.business.dto.ClubFPScoreDTO;
import be.bonamis.projects.umpire.business.dto.GameDTO;
import be.bonamis.projects.umpire.business.dto.IndividualResultDTO;
import be.bonamis.projects.umpire.business.dto.PersonResultDTO;
import be.bonamis.projects.umpire.business.dto.QuestionDTO;
import be.bonamis.projects.umpire.business.dto.QuestionStatDTO;
import be.bonamis.projects.umpire.business.dto.QuizDTO;
import be.bonamis.projects.umpire.business.dto.QuizStatsDTO;
import be.bonamis.projects.umpire.business.dto.ResponseStyle;
import be.bonamis.umpire.domain.history.Game;
import be.bonamis.umpire.domain.quiz.Answer;
import be.bonamis.umpire.domain.quiz.Question;
import be.bonamis.umpire.domain.quiz.Quiz;
import be.bonamis.umpire.history.ClubFPScore;
import be.bonamis.umpire.quiz.IndividualResult;
import be.bonamis.umpire.quiz.PersonResult;
import be.bonamis.umpire.quiz.QuestionStat;
import be.bonamis.umpire.quiz.QuizStat;

public class Mapper {

	public static QuizDTO quizToQuizDTO(Quiz quiz) {
		QuizDTO dto = new QuizDTO();
		dto.setDay(quiz.getCreationDay().toDate());
		dto.setId(quiz.getId());
		Collection<Question> coll = quiz.getQuestions();
		List<Boolean> corr = new ArrayList<Boolean>();
		List<QuestionDTO> qDtos = new ArrayList<QuestionDTO>();
		for (Question question : coll) {
			QuestionDTO qDto = new QuestionDTO();
			List<AnswerDTO> aDtos = new ArrayList<AnswerDTO>();
			for (Answer answer : question.getAnswers()) {
				aDtos.add(answerToanswerDto(answer));
				corr.add(answer.isCorrect());
			}
			qDto.setId(question.getId());
			qDto.setQuestionFr(question.getQuestionFr());
			qDto.setQuestionNl(question.getQuestionNl());
			qDto.setAnswers(aDtos);
			qDtos.add(qDto);
		}
		dto.setCorrects(corr);
		dto.setQuestions(qDtos);
		return dto;
	}

	public static QuizStatsDTO quizStatToDto(QuizStat quizStat) {
		QuizStatsDTO dto = new QuizStatsDTO();
		dto.setPersonResults(personResultsToDtoList(quizStat.getPersonResults()));
		dto.setQuestionStats(questionStatsToDtoList(quizStat.getQuestionStats()));
		return dto;
	}

	private static AnswerDTO answerToanswerDto(Answer answer) {
		AnswerDTO dto = new AnswerDTO();
		dto.setAnswerFr(answer.getAnswerFr());
		dto.setAnswerNl(answer.getAnswerNl());
		dto.setCorrect(answer.isCorrect());
		dto.setId(answer.getId());
		return dto;
	}

	private static List<QuestionStatDTO> questionStatsToDtoList(List<QuestionStat> questionStats) {
		List<QuestionStatDTO> list = new ArrayList<QuestionStatDTO>();
		for (QuestionStat questionStat : questionStats) {
			QuestionStatDTO dto = new QuestionStatDTO();
			dto.setQuestion(questionStat.getQuestion());
			dto.setResult(questionStat.getResult());
			list.add(dto);
		}
		return list;
	}

	private static List<PersonResultDTO> personResultsToDtoList(List<PersonResult> personResults) {
		List<PersonResultDTO> list = new ArrayList<PersonResultDTO>();
		for (PersonResult personResult : personResults) {
			PersonResultDTO dto = new PersonResultDTO();
			dto.setFullName(personResult.getFullName());
			dto.setScore(personResult.getScore());
			list.add(dto);
		}
		return list;
	}

	public static IndividualResultDTO resultToDto(IndividualResult result) {
		if (result != null) {
			IndividualResultDTO dto = new IndividualResultDTO();
			
			 List<Boolean> list = result.getResponses();
			 List<Integer> newList = new ArrayList<Integer>();
			 for (Boolean boolean1 : list) {
				newList.add(boolean1 ? 1:0);
			}
			dto.setResponses(newList);
			dto.setScore(result.getScore());
			dto.setStyles(domainResultToDto(result.getStyles()));
			return dto;
		} else {
			return null;
		}
	}

	private static ResponseStyle[] domainResultToDto(be.bonamis.umpire.quiz.ResponseStyle[] styles) {
		ResponseStyle[] array = new ResponseStyle[styles.length];
		for (int i = 0; i < styles.length; i++) {
			if (styles[i] != null) {
				array[i] = ResponseStyle.valueOf(styles[i].toString());
			}
		}
		return array;
	}

	public static GameDTO gameToGameDTO(Game game) {
		GameDTO dto = new GameDTO();
		dto.setAwayClub(game.getAwayClub().getName());
		dto.setGameDay(game.getGameDay().toDate());
		dto.setHomeClub(game.getHomeClub().getName());
		dto.setId(game.getId());
		dto.setType(game.getType().getDescriptionFr());
		dto.setUmpire01(game.getUmpire01().getLastName() + " " + game.getUmpire01().getFirstName());
		dto.setUmpire02(game.getUmpire02().getLastName() + " " + game.getUmpire02().getFirstName());
		return dto;
	}

	public static List<ClubFPScoreDTO> scoresToScoresDTO(List<ClubFPScore> scores) {
		List<ClubFPScoreDTO> list = new ArrayList<ClubFPScoreDTO>();
		for (ClubFPScore clubFPScore : scores) {
			ClubFPScoreDTO dto = new ClubFPScoreDTO();
			dto.setClubName(clubFPScore.getClubName());
			dto.setScore(clubFPScore.getScore());
			list.add(dto);
		}
		return list;
	}
}
